public class Test29 {
    public static int moreThanHalfNum(int[] number){
        if (number == null || number.length < 1) throw new IllegalArgumentException("invalid");
        int res = number[0];
        int count = 1;
        for (int i = 1;i<number.length;i++){
            if (count == 0){
                res = number[i];
                count  = 1;
            }else if (res == number[i]) count ++;
            else count --;
        }
        if (check(number,res)) return res;
        return -1;
    }

    public static boolean check(int[] number,int res){
        int count = 0;
        for (int num : number){
            if (res == num) count ++;
        }
        if (count > number.length/2) return true;
        return false;
    }
    public static void main(String[] args) {

        int numbers[] = {1, 2, 3, 2, 2, 2, 5, 4, 2};
        System.out.println(moreThanHalfNum(numbers));

        int numbers2[] = {2, 1, 2, 3, 5, 1, 3, 4, 5};
        System.out.println(moreThanHalfNum(numbers2));

    }
}
